package com.ycm.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ycm.dto.UserPageQueryDto;
import com.ycm.dto.UserSaveDto;
import com.ycm.entity.User;
import com.ycm.repository.UserRepository;
import com.ycm.vo.UserVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

/**
 * 用户服务
 *
 * @author ycm
 * @date 2024-11-13
 */
@Service
@Slf4j
public class UserService {
    @Resource
    private UserRepository userRepository;


    /**
     * 查询用户信息
     *
     * @param id 用户Id
     */
    @Cacheable(value = "getUserById", key = "#id") // 使用方法参数id作为缓存Key
    public UserVo getUserById(String id) {
        if (StrUtil.isBlank(id)) {
            return null;
        }

        // 查询数据库
        User user = userRepository.getById(id);

        // 将User对象转换为UserVo对象
        return BeanUtil.copyProperties(user, UserVo.class);
    }

    /**
     * 查询用户信息
     *
     * @param name 用户名
     */
    @Cacheable(value = "getUserByName", key = "#name") // 使用方法参数name作为缓存Key
    public UserVo getUserByName(String name) {
        if (StrUtil.isBlank(name)) {
            return null;
        }
        User user = userRepository.getUserByName(name);
        // 将User对象转换为UserVo对象
        return BeanUtil.copyProperties(user, UserVo.class);
    }

    /**
     * 保存用户
     */
    @Caching(evict = {
            @CacheEvict(value = "getUserById", allEntries = true),
            @CacheEvict(value = "getUserByName", allEntries = true),
            @CacheEvict(value = "getPageList", allEntries = true)
    })
    public void saveUser(UserSaveDto dto) {
        Assert.notNull(dto);
        User user = BeanUtil.copyProperties(dto, User.class);
        // 向数据库插入user记录
        userRepository.save(user);
    }

    /**
     * 分页查询用户列表
     */
    @Cacheable(value = "getPageList", keyGenerator = "keyGenerator") // 使用自定义缓存Key生成策略
    public IPage<UserVo> getPageList(UserPageQueryDto dto) {
        IPage<User> page = userRepository.getPageList(dto);
        Page<UserVo> pageInfo = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        pageInfo.setRecords(BeanUtil.copyToList(page.getRecords(), UserVo.class));
        return pageInfo;
    }

    /**
     * 删除用户
     *
     * @param id 用户id
     */
    @Caching(evict = {
            @CacheEvict(value = "getUserById", allEntries = true),
            @CacheEvict(value = "getUserByName", allEntries = true),
            @CacheEvict(value = "getPageList", allEntries = true)
    })
    public void deleteById(String id) {
        if (StrUtil.isBlank(id)) {
            return;
        }
        userRepository.removeById(id);
    }
}
